package com.share.example.selenium.web;

import com.share.common.common.Result;
import com.share.common.utils.DateExtUtil;
import com.share.common.utils.DateUtil;
import com.share.example.selenium.model.amazon.seller.ClosePromoteForm;
import com.share.example.selenium.model.amazon.seller.ClosePromoteTask;
import com.share.example.selenium.model.amazon.seller.PromoteForm;
import com.share.example.selenium.model.amazon.seller.PromoteTask;
import com.share.example.selenium.service.IBaseConfService;
import com.share.example.selenium.service.IPromoteDetailService;
import com.share.example.selenium.service.amazon.seller.AmazonSellerLoginService;
import com.share.example.selenium.service.amazon.seller.AmazonSellerPromotionService;
import com.share.example.selenium.utils.UsDateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/amazon/seller")
public class AmazonSellerController {
    private final int MAX_BENEFIT = 60;
    @Autowired
    private IBaseConfService baseConfService;
    @Autowired
    private AmazonSellerLoginService sellerLoginService;
    @Autowired
    private IPromoteDetailService promoteDetailService;
    @Autowired
    private AmazonSellerPromotionService sellerPromotionService;


    //打开卖家---填写表单页面
    @RequestMapping("/open/login/page")
    public Result openLoginPage() {
        return Result.bool(sellerLoginService.openLoginPage());
    }


    //填写卖家表单页面
    @RequestMapping("/fix/login/form")
    public Result fixLoginform(String account, String password) {
        return Result.bool(sellerLoginService.fixLoginform(account, password));
    }    //填写卖家表单页面

    @RequestMapping("/batchCreatePromotion")
    public Result batchCreatePromotion(String asin, String beginUsDateTime, String endUsDateTime, Integer benefit, Integer cycleNum, Integer orderWay) {
        Date curDate = new Date();
        Date date_16 = DateUtil.addHour(curDate, -16);
        if (StringUtils.isBlank(asin) || asin.length() < 6) {
            asin = baseConfService.getAsin();
            if (StringUtils.isBlank(asin)) {
                return Result.failure("输入的参数无效,asin={}" + asin);
            }
        }

        if (cycleNum == null || cycleNum < 1 || cycleNum > 10) {
            cycleNum = 2;
        }

        if (benefit == null || benefit < 1 || benefit > MAX_BENEFIT) {
            benefit = 15;
        }

        if (orderWay == null) {
            orderWay = 2;
        }

        //输入开始时间预处理
        Date startDate = null;
        if (StringUtils.isBlank(beginUsDateTime)) {
            startDate = curDate;
        } else {
            startDate = UsDateUtil.strDT2Date(beginUsDateTime);
        }

        //结束时间
        Date endDate = null;
        if (StringUtils.isBlank(endUsDateTime)) {
            endDate = DateUtil.getDayEnd(startDate);
        } else {
            endDate = UsDateUtil.strDT2Date(endUsDateTime);
        }

        String dateTimeParam = String.format("startDate=%s,endDate=%s", UsDateUtil.toYYYYMMDDHHMMSS(startDate), UsDateUtil.toYYYYMMDDHHMMSS(endDate));
        log.info("输入的美国时间{}", dateTimeParam);
        if ((startDate.getTime() >= date_16.getTime()) && (endDate.getTime() >= startDate.getTime())) {
            List<PromoteForm> promoteForms = sellerPromotionService.batchCreatePromotion(asin, benefit, startDate, endDate, cycleNum, orderWay);
            return Result.success(promoteForms);
        } else {
            return Result.failure("输入的时间无效," + dateTimeParam);
        }
    }


    @RequestMapping("/createPromotion")
    public Result createPromotion(String asin, String usDateTime, Integer benefit, String promotionSuffix) {
        Date curDate = new Date();
        Date date_16 = DateUtil.addHour(curDate, -16);
        if (StringUtils.isBlank(asin) || asin.length() < 6) {
            asin = baseConfService.getAsin();
            if (StringUtils.isBlank(asin)) {
                return Result.failure("输入的参数无效,asin={}" + asin);
            }
        }

        if (promotionSuffix == null) {
            promotionSuffix = "1";
        }

        if (benefit == null || benefit < 1 || benefit > MAX_BENEFIT) {
            benefit = 15;
        }

        Date inputDate = null;
        if (StringUtils.isBlank(usDateTime)) {
            inputDate = curDate;
        } else {
            inputDate = UsDateUtil.strDT2Date(usDateTime);
        }
        String dateTimeParam = String.format("usDateTime=%s,inputDate=%s,当前美国时间curDate=%s",
                usDateTime, UsDateUtil.toYYYYMMDDHHMMSS(inputDate), UsDateUtil.toYYYYMMDDHHMMSS(curDate));
        log.info("输入的美国时间是{}", dateTimeParam);
        if (inputDate.getTime() >= date_16.getTime()) {
            PromoteTask task = new PromoteTask(benefit, inputDate, promotionSuffix);
            task.setAsin(asin);
            task.setPromotionName(baseConfService.getPromoteNameByAsins(asin));
            //创建活动
            PromoteForm promotion = sellerPromotionService.createPromotion(task, true);

            //记录日志
            promoteDetailService.insertCreatePromotion(DateExtUtil.getDateTime(), task, promotion);
            if (promotion == null) {
                return Result.failure("创建活动失败");
            } else {
                return Result.success(promotion);
            }
        } else {
            return Result.failure("输入的时间无效," + dateTimeParam);
        }
    }

    //关闭单个促销活动
    @RequestMapping("/closePromotion")
    public Result closePromotion(String promotionLink) {
        if (StringUtils.isBlank(promotionLink) || !promotionLink.startsWith("http")) {
            log.info("输入促销活动的地址异常,promotionLink={}", promotionLink);
            return Result.failure("输入促销活动的地址异常,promotionLink=" + promotionLink);
        } else {
            return Result.bool(sellerPromotionService.closePromotion(DateExtUtil.getDateTime(), new ClosePromoteForm(promotionLink), true));
        }
    }

    //批量关闭促销
    @RequestMapping("/batchClosePromotion")
    public Result batchClosePromotion(Integer startPage, Integer dealSize, String realDlete, Integer orderNum, String promotionNames, String startFilter1, String startFilter2, String endFilter1, String endFilter2) {
        if (!"true".equals(realDlete)) {
            realDlete = "false";
        }

        if (startPage == null) {
            startPage = 3; //取值为1-9,默认取值为3
        } else {
            if (startPage >= 1 && startPage <= 9) {
                //超过这个值无法处理
            } else {
                startPage = 3; //取值为1-9,默认取值为3
            }
        }

        if (dealSize == null) {
            dealSize = 1; //默认取值为1
        } else {
            if (dealSize >= 1 && dealSize <= 40) { //最多1000页
                //超过这个值无法处理
            } else {
                dealSize = 1; //默认取值为1
            }
        }

        ClosePromoteTask task = new ClosePromoteTask();
        task.setStartPage(startPage);
        task.setDealSize(dealSize);

        task.setOrderNum(orderNum);
        task.setStartFilter1(startFilter1);
        task.setStartFilter2(startFilter2);
        task.setEndFilter1(endFilter1);
        task.setEndFilter2(endFilter2);
        if (promotionNames != null) {
            List<String> list = Arrays.stream(promotionNames.split(",")).filter(e -> StringUtils.isNotEmpty(e)).collect(Collectors.toList());
            task.setPromotionNames(list);
        }
        return Result.success(sellerPromotionService.batchClosePromotion(Boolean.parseBoolean(realDlete), task));
    }
}
